package proxy;

import com.itheima.service.UserService;
import com.itheima.service.impl.UserServiceImpl;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * @program: Spring_day03_04_综合案例
 * @description:
 * @author: zhanghz001
 * @create: 2020-11-29 17:49
 **/
public class UserServiceJDKProxy {

    //jdk代理的前提就是要需要原始对象有接口实现,才能对接口增强
    public static UserService createUserServiceJDKProxy(UserServiceImpl service) {
        ClassLoader classLoader = service.getClass().getClassLoader();
        Class<?>[] interfaces = service.getClass().getInterfaces();
        InvocationHandler h = new InvocationHandler() {
            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                Object invoke = method.invoke(service, args);
                System.out.println("jdk 代理的话就这样了");
                return invoke;
            }
        };
        UserService serviceProxy = (UserService) Proxy.newProxyInstance(
                classLoader
                , interfaces
                , h);
        return serviceProxy;
    }
}
